#include "../Comm/httplib.h"
#include "oj_control.hpp"
#include <string>
using namespace httplib;
using namespace ns_control;

static Control* p =nullptr;
void handler(int signo)
{
    p->Recovery();
}
int main()
{
    signal(3,handler);
    // 用户请求的服务器路由功能
    Server ser;
    Control ctrl;
    p = &ctrl;
    // 获取题目列表
    // 获取题目
    // 判题
    ser.Get("/all_questions", [&ctrl](const Request &req, Response &resp){
        std::string html;
        ctrl.AllQuestions(&html);
        resp.set_content(html, "text/html; charset=utf-8"); 
    });

    ser.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp){
        std::string number = req.matches[1];
        std::string html;
        ctrl.Question(number,&html);
        resp.set_content(html,"text/html; charset=utf-8"); 
    });

    ser.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp){
        std::string number = req.matches[1];
        std::string out_json;
        ctrl.Judge(number,req.body,&out_json);
        resp.set_content(out_json,"application/json; charset=utf-8");
    });

    ser.set_base_dir("./wwwroot");
    ser.listen("0.0.0.0", 8080);

    return 0;
}